package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.curriculum.PermissionLevel;
import com.edu.exam.enums.curriculum.PermissionScope;
import com.edu.exam.enums.curriculum.PermissionType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 附件权限实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：attachment_permissions
 *
 * 管理附件的访问权限控制，支持基于用户和角色的细粒度权限管理。
 * 确保附件数据的安全性。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("附件权限表")
@SQLDelete(sql = "UPDATE attachment_permissions SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class AttachmentPermission {

    /**
     * 权限记录唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 附件ID
     */
    @NotNull(message = "附件ID不能为空")
    @Column(name = "attachment_id", nullable = false)
    private Long attachmentId;

    /**
     * 用户ID
     */
    @Size(max = 50, message = "用户ID长度不能超过50个字符")
    @Column(name = "user_id", length = 50)
    private String userId;

    /**
     * 角色ID
     */
    @Size(max = 50, message = "角色ID长度不能超过50个字符")
    @Column(name = "role_id", length = 50)
    private String roleId;

    /**
     * 权限类型
     */
    @NotNull(message = "权限类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "permission_type", nullable = false, length = 20)
    private PermissionType permissionType;

    /**
     * 授权人ID
     */
    @NotNull(message = "授权人ID不能为空")
    @Size(max = 50, message = "授权人ID长度不能超过50个字符")
    @Column(name = "granted_by", nullable = false, length = 50)
    private String grantedBy;

    /**
     * 授权人姓名
     */
    @Size(max = 100, message = "授权人姓名长度不能超过100个字符")
    @Column(name = "granted_by_name", length = 100)
    private String grantedByName;

    /**
     * 授权时间
     */
    @NotNull(message = "授权时间不能为空")
    @Column(name = "grant_time", nullable = false)
    private LocalDateTime grantTime;

    /**
     * 过期时间
     */
    @Column(name = "expire_time")
    private LocalDateTime expireTime;

    /**
     * 是否有效
     */
    @Column(name = "is_active")
    @Builder.Default
    private Boolean isActive = true;

    /**
     * 权限级别
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "permission_level", length = 20)
    private PermissionLevel permissionLevel;

    /**
     * 权限范围
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "permission_scope", length = 20)
    private PermissionScope permissionScope;

    /**
     * 权限描述
     */
    @Size(max = 500, message = "权限描述长度不能超过500个字符")
    @Column(name = "permission_description", length = 500)
    private String permissionDescription;

    /**
     * 访问限制条件
     */
    @Size(max = 1000, message = "访问限制条件长度不能超过1000个字符")
    @Column(name = "access_conditions", columnDefinition = "TEXT")
    private String accessConditions;

    /**
     * 权限标签
     */
    @Size(max = 200, message = "权限标签长度不能超过200个字符")
    @Column(name = "permission_tags", length = 200)
    private String permissionTags;

    /**
     * 权限元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 最后访问时间
     */
    @Column(name = "last_access_time")
    private LocalDateTime lastAccessTime;

    /**
     * 访问次数
     */
    @Column(name = "access_count")
    @Builder.Default
    private Integer accessCount = 0;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        if (this.grantTime == null) {
            this.grantTime = now;
        }
    }

  
}